﻿using System;
using System.Data;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using PanasonicWarranty.Business;

namespace PanasonicWarranty.UI
{
    public partial class ChangeSetting : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (SqlConnection conn = new SqlConnection(LocalSetting.ConnectionString))
                {
                    conn.Open();
                    string query = "SELECT * FROM pepsi_User WHERE UserID = " + CurUser.UserID;
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = query;
                    SqlDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        txtEmail.Text = dr["Email"].ToString();
                        textWebSiteURL.Text = dr["WebSiteURL"].ToString();
                        drdGender.SelectedValue = CurUser.Gender.ToString();
                        TextCountry.Text = dr["Country"].ToString();
                        TextState.Text = dr["State"].ToString();
                        TextSignature.Text = dr["Signature"].ToString();
                        TextFirstName.Text = dr["FirstName"].ToString();
                        TextLastName.Text = dr["LastName"].ToString();
                    }
                    conn.Close();
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            RedirectToPage(DefaultURL);
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            using (SqlConnection conn = new SqlConnection(LocalSetting.ConnectionString))
            {
                conn.Open();
                string query = "UPDATE pepsi_User SET Email = @Email, WebSiteURL = @WebSiteURL, Country = @Country, State = @State, Signature = @Signature, FirstName = @FirstName, LastName = @LastName WHERE UserID = " + CurUser.UserID;
                using (SqlCommand cmd = new SqlCommand(query, conn))
                {
                    cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
                    cmd.Parameters.AddWithValue("@Gender", Convert.ToBoolean(drdGender.SelectedValue));
                    cmd.Parameters.AddWithValue("@WebSiteURL", textWebSiteURL.Text);
                    cmd.Parameters.AddWithValue("@Country", TextCountry.Text);
                    cmd.Parameters.AddWithValue("@State", TextState.Text);
                    cmd.Parameters.AddWithValue("@Signature", TextSignature.Text);
                    cmd.Parameters.AddWithValue("@FirstName", TextFirstName.Text);
                    cmd.Parameters.AddWithValue("@LastName", TextLastName.Text);
                    cmd.ExecuteNonQuery();
                    UploadImage();
                }
                conn.Close();
            }
        }

        protected void UploadImage()
        {
            try
            {
                string sFolderPath = MapPath("~/UploadImages/");
                HttpPostedFile myFile = FileUpload1.PostedFile;
                System.Drawing.Image bm = System.Drawing.Image.FromStream(myFile.InputStream);
                bm = ResizeBitmap((Bitmap)bm, 50, 55); /// new width, height
                bm.Save(Path.Combine(sFolderPath, myFile.FileName));
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
        
        private Bitmap ResizeBitmap(Bitmap b, int nWidth, int nHeight)
        {
            Bitmap result = new Bitmap(nWidth, nHeight);
            using (Graphics g = Graphics.FromImage((System.Drawing.Image)result))
                g.DrawImage(b, 0, 0, nWidth, nHeight);
            return result;
        }
    }
}